package com.loukou.lkt.cron.task;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.loukou.lkt.lkt.dao.PoPurchaseOrderDao;
import com.loukou.lkt.lkt.entity.PoPurchaseOrder;
import com.loukou.lkt.lkt.enums.PoPurchaseorderStateEnum;
import com.loukou.lkt.lkt.enums.SettlementTypeEnum;
import com.loukou.lkt.lkt.resp.RespDto;
import com.loukou.lkt.lkt.service.PoPurchaseOrderService;
/**
 * 推送采购入库（自动推送）
 * @author 荣
 *
 */
@Service("SendPurchaseStockInOrderTask")
public class SendPurchaseStockInOrderTask{
	Logger logger = Logger.getLogger(SendPurchaseStockInOrderTask.class);
	
	@Autowired
	private PoPurchaseOrderDao poPurchaseOrderDao;
	
	@Autowired
	private PoPurchaseOrderService poPurchaseOrderService;
	
	public void noticeWmsIn() {
		List<PoPurchaseOrder> orderList=new ArrayList<PoPurchaseOrder>();
		
		//获取有预付款需要财务审核的订单
		List<PoPurchaseOrder> financeOrderList=poPurchaseOrderDao.findByOrderStateAndSettmentType(PoPurchaseorderStateEnum.STATE_FINANCE_CHECKED.getState(),SettlementTypeEnum.TYPE_AdvPay.getId());
		if(financeOrderList!=null && financeOrderList.size()>0){
			orderList.addAll(financeOrderList);
		}
		
		//获取没有预付款不需要财务审核的订单
		List<PoPurchaseOrder> noFinanceOrderList=poPurchaseOrderDao.findByOrderStateAndSettmentTypeNot(PoPurchaseorderStateEnum.STATE_DEPART_CHECKED.getState(),SettlementTypeEnum.TYPE_AdvPay.getId());
		if(noFinanceOrderList!=null && noFinanceOrderList.size()>0){
			orderList.addAll(noFinanceOrderList);
		}
		
		if(orderList!=null && orderList.size()>0){
			for(PoPurchaseOrder o:orderList){
				RespDto<String> result=poPurchaseOrderService.sendPurchaseOrderToWms(o.getId());
				if (result.getCode()!=200) {
					logger.error("send purchase order error!");
					logger.error("推送采购单id【" + o.getId() + "】失败，错误:"
							+ result.getMsg());
					continue;
				}
			}
		}
	}
}
